package hospital;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class ChatServer {
    //Server
    public static void main(String[] args) {
        try {
            ServerSocket st=new ServerSocket(8888);
            System.out.println("监听在8888端口");
            while (true){
                Socket s = st.accept();//建立连接
                //new ChatThread(s).start();
                //new FileThread(s).start();
                new FileThread2(s).start();

//                System.out.println("s:"+s.getInetAddress()+",端口："+s.getPort()+",本地端口："+s.getLocalPort());
//                InputStream in = s.getInputStream();
//                OutputStream out = s.getOutputStream();
//                BufferedReader br=new BufferedReader(new InputStreamReader(in));
//                PrintWriter pw=new PrintWriter(out,true);
//                while (true){
//                    //你问我答
//                    String ques=br.readLine();
//                    System.out.println("有人问："+ques);
//                    pw.println("我知道了："+ques+",稍后回复！");
//                    if("exit".equals(ques))
//                        break;
//                }
//                s.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

class  ChatThread extends  Thread{
    private final Socket s;

    public ChatThread(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        System.out.println("s:"+s.getInetAddress()+",端口："+s.getPort()+",本地端口："+s.getLocalPort());
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            PrintWriter pw=new PrintWriter(out,true);
            while (true){
                //你问我答
                String ques=br.readLine();
                System.out.println("有人问："+ques);
                pw.println("我知道了："+ques+",稍后回复！");
                if(ques==null)
                    break;
                if("exit".equals(ques))
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

class FileThread extends Thread{
    private final Socket s;
    public FileThread(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        try {
            System.out.println(s+"链接了");
            FileInputStream fin=new FileInputStream("D:\\Documents\\图片和资料\\job-salary.png");
            byte []b=new byte[fin.available()];
            fin.read(b);
            fin.close();
            OutputStream out = s.getOutputStream();
            out.write(b);
            out.flush();
            System.out.println("文件发送成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                s.close();
            } catch (IOException e) {

            }
        }
    }
}
class FileThread2 extends Thread{
    private final Socket s;
    public FileThread2(Socket s){
        this.s=s;
    }
    public static final String PATH="E:\\files\\1.0衢州原型定稿09.07\\1.0衢州原型定稿09.07\\images\\实人认证-原生";
    @Override
    public void run() {
        try {
            System.out.println(s+"链接了");
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String fileName=br.readLine();

            FileInputStream fin=new FileInputStream(new File(PATH,fileName));
            byte []b=new byte[fin.available()];
            fin.read(b);
            fin.close();

            out.write(b);
            out.flush();
            System.out.println("文件"+fileName+"发送成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                s.close();
            } catch (IOException e) {

            }
        }
    }
}